<!DOCTYPE html>
<!--suppress ALL-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous" />
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>
<body>
<div class="container-fluid" id="app">
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-12 text-center" style="background-color: red; color: white" >
                <h3>蜗牛书店</h3>
            </div>
        </div>
    </div>

    <div class="container">

        <div class="row">
            <div class="col-md-4 col-md-offset-4" style="border: 1px solid #cccccc ;border-radius: 5px;margin-top: 10px">
                <p th:text="${errorInfo}" th:if="${!#strings.isEmpty(errorInfo)}"></p>
                <form>
                    <div class="form-group">
                        <label for="account">用户名:</label>
                        <input type="text" name="username" class="form-control" id="account" placeholder="请输入用户名">
                    </div>
                    <div class="form-group">
                        <label for="password">密码</label>
                        <input type="password" name="password" class="form-control" id="password" placeholder="请输入密码">
                    </div>
                    <div class="form-group">
                        <label for="repass">确认密码:</label>
                        <input type="password" name="repass" class="form-control" id="repass" placeholder="请输入确认密码">
                    </div>

                    <div class="form-group">
                        <label for="email">邮箱:</label>
                        <input type="email" name="email" class="form-control" id="email" placeholder="请输入邮箱">
                        <button class="btn btn-primary" @click.prevent="sendEmail">发送验证码</button>
                    </div>
                    <div class="form-group">
                        <label for="emailCode">请输入验证码:</label>
                        <input type="text" name="emailCode" class="form-control" id="emailCode" placeholder="请输入邮箱验证码">
                    </div>

                    <button type="button" class="btn btn-default" @click.prevent="reg">注册</button> <a th:href="@{/login.html}">去登录</a>
                </form>
            </div>
        </div>

    </div>

    <script>
        let vueApp = new Vue({
            el:"#app" ,
            data:{

            },
            methods:{
                sendEmail(){
                    let email = $("#email").val();
                    let url = "/user/sendEmail";

                    $.post(url,{"email":email},function (data){
                        if (data == 'ok') {
                            alert("邮件发送成功!")
                        }
                    });
                },
                reg(){
                    let url = "/user/reg";
                    let params = {
                        "account":$("#account").val(),
                        "password":$("#password").val(),
                        "repass":$("#repass").val(),
                        "email":$("#email").val(),
                        "emailCode":$("#emailCode").val()
                    };
                    $.post(url,params,function (data){
                        if (data == 'ok') {
                            alert("注册成功，5秒后跳转到登录界面!")
                            //定时器
                            setTimeout("location.href='/login.html'",5000);
                        }else {
                            alert(data)
                        }
                    });
                }
            }
        })
    </script>

    <div class="container">

    </div>
</div>

</body>
</html>